﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Lib.Helper;
using MSIACCSL2.AppParamServices;
using MSIACCSL2.Command;
using MSIACCSL2.CostCenterServices;
using MSIACCSL2.GroupServices;
using MSIACCSL2.ItemServices;
using MSIACCSL2.SubGroupServices;
using MSIACCSL2.WarehouseServices;
using WcfService1.DTO;
using System.ComponentModel;
using System.Linq;

namespace MSIACCSL2.ViewModels
{
    public class ReportPembelianViewModel : ViewModelBase
    {
        private ObservableCollection<CostCenterDTO> _CostCenters;
        private DateTime? _Periode;
        private DateTime? _Periode2;
        private AppParamServicesClient paramClient;
        private string ReportUrl;
        private CostCenterServicesClient costCenterClient;
        private ObservableCollection<string> _Options;
        private CostCenterDTO _CostCenter;
        private string _Option;


        public ReportPembelianViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                paramClient = new AppParamServicesClient();
                costCenterClient  = new CostCenterServicesClient();
                ShowCommand = new DelegateCommand(Show, CanshowCommand);
                GetServerUrl();
                GetCostCenters();
                Options = new ObservableCollection<string>();
                Options.Add("Subgroup");
                Options.Add("Item");
                Option = Options.FirstOrDefault();
            }
        }

     
        
        public DateTime? Periode
        {
            get { return _Periode; }
            set
            {
                if (Periode == value) return;
                _Periode = value;
                FirePropertyChanged("Periode");
            }
        }

        public DateTime? Periode2
        {
            get { return _Periode2; }
            set
            {
                if (Periode2 == value) return;
                _Periode2 = value;
                FirePropertyChanged("Periode2");
            }
        }

        public ObservableCollection<CostCenterDTO> CostCenters
        {
            get { return _CostCenters; }
            set
            {
                if (CostCenters == value) return;
                _CostCenters = value;
                FirePropertyChanged("CostCenters");
            }
        }

        public ObservableCollection<string > Options
        {
            get { return _Options; }
            set
            {
                if (Options == value) return;
                _Options = value;
                FirePropertyChanged("Options");
            }
        }

        public CostCenterDTO CostCenter
        {
            get { return _CostCenter; }
            set
            {
                if (CostCenter == value) return;
                _CostCenter = value;
                FirePropertyChanged("CostCenter");
            }
        }


        public string Option
        {
            get { return _Option; }
            set
            {
                if (Option == value) return;
                _Option = value;
                FirePropertyChanged("Option");
            }
        }

        public ICommand ShowCommand { get; set; }

        public bool CanshowCommand(Object param)
        {
            return true;
        }


        public void GetServerUrl()
        {
            paramClient.GetAppParamListCompleted += new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted);
            paramClient.GetAppParamListAsync("ReportUrl");
        }

        void paramClient_GetAppParamListCompleted(object sender, GetAppParamListCompletedEventArgs e)
        {
            paramClient.GetAppParamListCompleted -= new EventHandler<GetAppParamListCompletedEventArgs>(paramClient_GetAppParamListCompleted);
            ReportUrl = e.Result.FirstOrDefault().AppParamValue;
        }

        public void GetCostCenters()
        {
            costCenterClient.GetAllCostCenterCompleted += new EventHandler<GetAllCostCenterCompletedEventArgs>(costCenterClient_GetAllCostCenterCompleted);
            costCenterClient.GetAllCostCenterAsync();
        }

        void costCenterClient_GetAllCostCenterCompleted(object sender, GetAllCostCenterCompletedEventArgs e)
        {
            costCenterClient.GetAllCostCenterCompleted -= new EventHandler<GetAllCostCenterCompletedEventArgs>(costCenterClient_GetAllCostCenterCompleted);
            CostCenters = e.Result;
            CostCenters.Insert(0,new CostCenterDTO());
        }
      

        public void Show(Object param)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Select  a.PINo AS SINo,a.PIDate AS InvoiceDate,a.Term,a.Kurs AS Rate,a.Ppn,a.[Description] AS Keterangan,a.Discount,a.CurrencyCode,  ");
            sb.Append("(Case When a.DiscountType is Null then '' Else a.DiscountType End) as DiscountType  ,  ");
            sb.Append("sr.PRNo AS SRNo,  (Case When sr.PRNo IS NULL THEN b.Qty ELSE 0 - srd.Qty END) AS Qty,    ");
            sb.Append("b.Price,b.Discount as DiscountD,  (Case When b.DiscountType is Null then '' Else b.DiscountType End) as DiscountTypeD,    ");
            sb.Append("c.SupplierName AS CustomerName,c.Address,c.ContactPerson,c.Telp,c.Fax,item.ItemName,s.SubgroupName,i.SatuanUnit     ");
            sb.Append("from PurchaseInvoice a inner Join PurchaseInvoiceDetail b on a.PINO = b.PINO     ");
            sb.Append("Left Join Supplier c on c.SupplierId = a.SupplierId      ");
            sb.Append("Left Join Inventory i on i.SerialNumber = b.SerialNumber   ");
            sb.Append("Left Join Item item on item.ItemId = i.ItemId   ");
            sb.Append("Left Join Subgroup s on s.SubGroupID = item.SubgroupId   ");
            sb.Append("LEFT JOIN PurchaseReturn sr ON sr.PRNo = a.PINo  ");
            sb.Append("LEFT JOIN PurchaseReturnDetail srd ON srd.PRNo = sr.PRNo  ");
            sb.AppendFormat("Where a.PIDate between '{0}' AND '{1}' ",Periode.Value.ToString("yyyy-MM-dd"),Periode2.Value.ToString("yyyy-MM-dd"));
            
            sb.Append("Order By a.PIDate,s.SubgroupId,i.ItemId ");

            String query = "LapPembelian.aspx?sql=" + System.Windows.Browser.HttpUtility.UrlEncode(sb.ToString());
            query = string.Concat(query, String.Format("&TglStart={0}", Periode.Value.ToString("dd-MM-yyyy")));
            query = string.Concat(query, String.Format("&TglEnd={0}", Periode2.Value.ToString("dd-MM-yyyy")));
            query = string.Concat(query, String.Format("&Tipe={0}",Option));

            MyButton button = new MyButton(ReportUrl + query);
            button.ClickMe();
        }

    }
}
